﻿using UnityEngine;
using System;
using BestHTTP;
using UnityEngine.UI;
using System.Text.RegularExpressions;

//自建后台注册
public class HTTPRegister : MonoBehaviour
{
    public Text idText;
    public Text passText;
    public Text defeatText;
    public Button registerBtn;
    public Button backBtn;
    public GameObject loginObj;
    
    private float time = 0;
    private bool isDoTweenAni = false;
    bool quiting = false;
    float quitTime = 0;

    private void Start()
    {
        registerBtn.onClick.AddListener(()=> {
            StudentRegister();
        });
        backBtn.onClick.AddListener(() => {
            ChangePanel();
        });
    }

    public void StudentRegister()
    {
        if (quiting)
            return;
        regist(idText.text, passText.text);
    }

    public void regist(string username, string passward)
    {
        CJ.XNFZ.Server.XNFZServerHelper.Instance.Regist(
            username,
            passward,
            () => {
                Debug.Log("ok");
                defeatText.text = "注册成功";
                defeatText.gameObject.SetActive(true);
                isDoTweenAni = true;
                quiting = true;
                quitTime = Time.time + 2;
            },
            (string e) => {
                Debug.Log(e);
                defeatText.text = e;
                defeatText.gameObject.SetActive(true);
                isDoTweenAni = true;
            }

        );
    }

    private void StudentRegisterResponse(HTTPRequest request, HTTPResponse response)
    {
        int responseState = response.StatusCode;
        print("Register:" + responseState);
        if (responseState == 200) {
            //注册成功
            ChangePanel();
        } else {
            //注册失败，飘字
            defeatText.gameObject.SetActive(true);
            isDoTweenAni = true;
        }
    }

    private void ChangePanel()
    {
        GameObject obj = Instantiate(loginObj);
        obj.SetActive(true);
        obj.transform.position = Vector3.zero;
        Destroy(gameObject);
    }

    private void Update()
    {
        if(quiting && Time.time>quitTime)
        {
            quiting = false;
            ChangePanel();
        }

        if (isDoTweenAni) {
            time += Time.deltaTime;
            if (time <= 2) {
                defeatText.transform.localPosition += Vector3.Lerp(Vector3.zero, Vector3.up, 1f);
            } else {
                isDoTweenAni = false;
                time = 0;
                defeatText.gameObject.SetActive(false);
                defeatText.transform.localPosition = Vector3.zero;
            }
        }
    }

    //private bool isChinese(char c)
    //{
    //    return c >= 0x4E00 && c <= 0x9FA5;
    //}

    //public bool checkString(string str)
    //{
    //    char[] ch = str.ToCharArray();
    //    if (str != null) {
    //        for (int i = 0; i < ch.Length; i++) {
    //            if (isChinese(ch[i])) {
    //                return true;
    //            }
    //        }
    //    }
    //    return false;
    //}

    //void AccountVenify()
    //{
    //    string patter = "^[a-zA-Z][a-zA-Z\d!@#$%^&*.,_+-=]{6,9}$";
    //    string account1 = "a123456@..";
    //    string account2 = "123456@..";
    //    Regex r = new Regex(patter);
    //    Debug.Log(r.IsMatch(account1));//true
    //    Debug.Log(r.IsMatch(account2));//false
    //}

    bool CheckString(string id, string pass)
    {
        Regex rex = new Regex(@"[\u4E00-\u9FFF]");
        var idResult = rex.Match(id);
        var passResult = rex.Match(pass);
        if (idResult.Success || passResult.Success) {
            return true;
        }

        Regex rex2 = new Regex(@"^\w+$");
        idResult = rex2.Match(id);
        passResult = rex2.Match(pass);
        if (!idResult.Success || !passResult.Success) {
            return true;
        }
        return false;
    }
}